﻿unit CmPackageAccountModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm;

type

  TCmPackageAccountModel = class(TObject)
  public 
    const cModuleKey='CmPackageAccount'; 
    const cTblName ='cm_package_account';
    const cBaseView = 'view_cm_package_account';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cPackageAccountIdKey='package_account_id';
    const cCustomerIdKey='customer_id';
    const cPackageTypeIdKey='package_type_id';
    const cPackageAccountTypeKey='package_account_type';
    const cPackageAccountDirectionKey='package_account_direction';
    const cPackageAccountQtyKey='package_account_qty';
    const cOrgIdKey='org_id';
    const cPackageAccountRemarkKey='package_account_remark';
    const cPackageAccountRefTypeKey='package_account_ref_type';
    const cPackageAccountRefIdKey='package_account_ref_id';
    const cPackageAccountCrtDateKey='package_account_crt_date';
  end;
  
  TCmPackageAccount = class(TObject)
  private    
    fPackageAccountId:string;
    fCustomerId:string;
    fPackageTypeId:string;
    fPackageAccountType:Integer;
    fPackageAccountDirection:Integer;
    fPackageAccountQty:Real;
    fOrgId:string;
    fPackageAccountRemark:string;
    fPackageAccountRefType:Integer;
    fPackageAccountRefId:string;
    fPackageAccountCrtDate:TDateTime;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property PackageAccountId:string read fPackageAccountId write fPackageAccountId;
    property CustomerId:string read fCustomerId write fCustomerId;
    property PackageTypeId:string read fPackageTypeId write fPackageTypeId;
    property PackageAccountType:Integer read fPackageAccountType write fPackageAccountType;
    property PackageAccountDirection:Integer read fPackageAccountDirection write fPackageAccountDirection;
    property PackageAccountQty:Real read fPackageAccountQty write fPackageAccountQty;
    property OrgId:string read fOrgId write fOrgId;
    property PackageAccountRemark:string read fPackageAccountRemark write fPackageAccountRemark;
    property PackageAccountRefType:Integer read fPackageAccountRefType write fPackageAccountRefType;
    property PackageAccountRefId:string read fPackageAccountRefId write fPackageAccountRefId;
    property PackageAccountCrtDate:TDateTime read fPackageAccountCrtDate write fPackageAccountCrtDate;
  end;

  TCmPackageAccountList = class(TDictionary<string,TCmPackageAccount>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TCmPackageAccount;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TCmPackageAccount):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TCmPackageAccount;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TCmPackageAccountModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmPackageAccountModel.cTblName;
    dblmTable: ReadName:=TCmPackageAccountModel.cTblName;
    dblmView: ReadName:=TCmPackageAccountModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TCmPackageAccountModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmPackageAccountModel.cTblName;
    dblmTable: ReadName:=TCmPackageAccountModel.cTblName;
    dblmView: ReadName:=TCmPackageAccountModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TCmPackageAccountModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TCmPackageAccountModel.cTblName;
    dblmTable: ReadName:=TCmPackageAccountModel.cTblName;
    dblmView: ReadName:=TCmPackageAccountModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TCmPackageAccount.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TCmPackageAccount.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TCmPackageAccount.ResetObject;
begin
  fObjGUID:=TSK0001Comm.GetGUID;
  fPackageAccountId:=EmptyStr;
  fCustomerId:=EmptyStr;
  fPackageTypeId:=EmptyStr;
  fPackageAccountType:=-1;
  fPackageAccountDirection:=-1;
  fPackageAccountQty:=-1;
  fOrgId:=EmptyStr;
  fPackageAccountRemark:=EmptyStr;
  fPackageAccountRefType:=-1;
  fPackageAccountRefId:=EmptyStr;
  fPackageAccountCrtDate:=fInitDate;
end;

function TCmPackageAccount.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountIdKey)<>nil) then
    begin
      fPackageAccountId:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountIdKey).AsString;     
      fObjGUID:=TSK0001Comm.SetObjGUID(fPackageAccountId);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cCustomerIdKey)<>nil) then
    begin
      fCustomerId:=iCds.FieldByName(TCmPackageAccountModel.cCustomerIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cCustomerIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageTypeIdKey)<>nil) then
    begin
      fPackageTypeId:=iCds.FieldByName(TCmPackageAccountModel.cPackageTypeIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountTypeKey)<>nil) then
    begin
      fPackageAccountType:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountTypeKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountDirectionKey)<>nil) then
    begin
      fPackageAccountDirection:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountDirectionKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountDirectionKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountQtyKey)<>nil) then
    begin
      fPackageAccountQty:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountQtyKey).AsFloat;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountQtyKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cOrgIdKey)<>nil) then
    begin
      fOrgId:=iCds.FieldByName(TCmPackageAccountModel.cOrgIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountRemarkKey)<>nil) then
    begin
      fPackageAccountRemark:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountRefTypeKey)<>nil) then
    begin
      fPackageAccountRefType:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountRefTypeKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountRefTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountRefIdKey)<>nil) then
    begin
      fPackageAccountRefId:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountRefIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountRefIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountCrtDateKey)<>nil) then
    begin
      if iCds.FindField(TCmPackageAccountModel.cPackageAccountCrtDateKey).IsNull then
      begin
        fPackageAccountCrtDate:=fInitDate;
      end else begin
        fPackageAccountCrtDate:=iCds.FieldByName(TCmPackageAccountModel.cPackageAccountCrtDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountCrtDateKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmPackageAccount.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageAccountIdKey).AsString:=fPackageAccountId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cCustomerIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cCustomerIdKey).AsString:=fCustomerId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cCustomerIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageTypeIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageTypeIdKey).AsString:=fPackageTypeId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountTypeKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageAccountTypeKey).AsInteger:=fPackageAccountType;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountDirectionKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageAccountDirectionKey).AsInteger:=fPackageAccountDirection;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountDirectionKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountQtyKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageAccountQtyKey).AsFloat:=fPackageAccountQty;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountQtyKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cOrgIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cOrgIdKey).AsString:=fOrgId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageAccountRemarkKey).AsString:=fPackageAccountRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountRefTypeKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageAccountRefTypeKey).AsInteger:=fPackageAccountRefType;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountRefTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountRefIdKey)<>nil) then
    begin
      iCds.FieldByName(TCmPackageAccountModel.cPackageAccountRefIdKey).AsString:=fPackageAccountRefId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountRefIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TCmPackageAccountModel.cPackageAccountCrtDateKey)<>nil) then
    begin
      if SameDateTime(fPackageAccountCrtDate,fInitDate) then
      begin
        iCds.FieldByName(TCmPackageAccountModel.cPackageAccountCrtDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TCmPackageAccountModel.cPackageAccountCrtDateKey).AsDateTime:=fPackageAccountCrtDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TCmPackageAccountModel.cPackageAccountCrtDateKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmPackageAccount.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmPackageAccount.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TCmPackageAccountModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TCmPackageAccountModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TCmPackageAccountModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TCmPackageAccountModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TCmPackageAccountModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TCmPackageAccountModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TCmPackageAccountModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TCmPackageAccountModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TCmPackageAccountModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('PackageAccountId')<>nil then
    begin
      fPackageAccountId:=Json.ItemByName('PackageAccountId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('CustomerId')<>nil then
    begin
      fCustomerId:=Json.ItemByName('CustomerId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'CustomerId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageTypeId')<>nil then
    begin
      fPackageTypeId:=Json.ItemByName('PackageTypeId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageTypeId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageAccountType')<>nil then
    begin
      fPackageAccountType:=Json.ItemByName('PackageAccountType').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountType']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageAccountDirection')<>nil then
    begin
      fPackageAccountDirection:=Json.ItemByName('PackageAccountDirection').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountDirection']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageAccountQty')<>nil then
    begin
      fPackageAccountQty:=Json.ItemByName('PackageAccountQty').AsFloat;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountQty']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrgId')<>nil then
    begin
      fOrgId:=Json.ItemByName('OrgId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrgId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageAccountRemark')<>nil then
    begin
      fPackageAccountRemark:=Json.ItemByName('PackageAccountRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountRemark']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageAccountRefType')<>nil then
    begin
      fPackageAccountRefType:=Json.ItemByName('PackageAccountRefType').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountRefType']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageAccountRefId')<>nil then
    begin
      fPackageAccountRefId:=Json.ItemByName('PackageAccountRefId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountRefId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageAccountCrtDate')<>nil then
    begin
      fPackageAccountCrtDate:=Json.ItemByName('PackageAccountCrtDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageAccountCrtDate']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TCmPackageAccount.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TCmPackageAccountModel.cModuleKey;
    Json.Add(TCmPackageAccountModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TCmPackageAccountModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TCmPackageAccountModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('PackageAccountId').AsString:=fPackageAccountId;
    Json.Add('CustomerId').AsString:=fCustomerId;
    Json.Add('PackageTypeId').AsString:=fPackageTypeId;
    Json.Add('PackageAccountType').AsInteger:=fPackageAccountType;
    Json.Add('PackageAccountDirection').AsInteger:=fPackageAccountDirection;
    Json.Add('PackageAccountQty').AsFloat:=fPackageAccountQty;
    Json.Add('OrgId').AsString:=fOrgId;
    Json.Add('PackageAccountRemark').AsString:=fPackageAccountRemark;
    Json.Add('PackageAccountRefType').AsInteger:=fPackageAccountRefType;
    Json.Add('PackageAccountRefId').AsString:=fPackageAccountRefId;
    Json.Add('PackageAccountCrtDate').AsDateTime:=fPackageAccountCrtDate;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TCmPackageAccountList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TCmPackageAccountList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TCmPackageAccountList.ResetObject;
begin
  Clear;
end;

function TCmPackageAccountList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TCmPackageAccount;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TCmPackageAccount.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmPackageAccountList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmPackageAccountList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TCmPackageAccount;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TCmPackageAccountModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TCmPackageAccountModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TCmPackageAccountModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TCmPackageAccountModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TCmPackageAccountModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TCmPackageAccountModel.cCdsModelKey)<>nil then
      begin
        TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TCmPackageAccountModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TCmPackageAccountModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TCmPackageAccount.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TCmPackageAccountList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TCmPackageAccountModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TCmPackageAccountModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TCmPackageAccountModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TCmPackageAccountList.FindByKey(const iKey:string):TCmPackageAccount;
var
  Item:TCmPackageAccount;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TCmPackageAccountList.IsExist(iKey:string):Boolean;
var
  Item:TCmPackageAccount;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TCmPackageAccountList.Add(const iItem:TCmPackageAccount):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TCmPackageAccountList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TCmPackageAccountList.Remove(const iKey:string):TCmPackageAccount;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TCmPackageAccountList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
